package com.example.demo.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.service.CinemaService;
import com.woniuxy.movice.entity.Cinema;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import net.bytebuddy.asm.Advice.This;
@Api("分影院的增删改查")
@RestController
@Data
@RequestMapping("/cinema")
/**
 * 
 * @author 陈俊名
 * 1.查询所有分院的信息
 * 2.根据id查询分影院的详细信息
 * 3.根据 id删除指定的分影院
 */
public class CinemaController {
	 String newFileName="";
	@Resource
	private CinemaService cinemaService;
	@ApiOperation(value="查询所有分影院信息（不需要参数）",notes="返回所有影院信息")
	@RequestMapping("/all")
	@ResponseBody
	public List<Cinema> findCinema(){
		System.out.println("查看所有");
		return cinemaService.findAllCinemas();
	}
	@ApiOperation(value="根据id查询分影院的详细信息(参数id：1，2，3)",notes="返回指定影院详细信息")
	@RequestMapping("/id")
	@ResponseBody
	public Cinema findIdByCinema(Integer id) {
		System.out.println("11111");
		return cinemaService.findIdByCinema(id);
	}
	@ApiOperation(value="根据id删除分影院(参数id例：1，2，3)",notes="无返回值")
	@RequestMapping("/delete")
	@ResponseBody
	public void deleteIdByCinema(Integer id) {
		cinemaService.deleteIdByCinema(id);
	}
	//影院多图片上传
	@ApiOperation(value="分影院图片上传(参数：本地图片)",notes="返回图片地址")
	@RequestMapping("/upload")
	@ResponseBody
	public String handleFileUpload(HttpServletRequest request) {
	    MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
	    List<MultipartFile> files = (params.getFiles("file"));
		//接收前端传过来的字段
	    //String name = params.getParameter("name");
	    //System.out.println(re);
	    MultipartFile file = null;
	    //先设置一个数组来装file路径
	    List imgs = new ArrayList();
	   //设置图片路径
		String UPLOAD_FOLDER = "target/classes/static/upload/";
	    for (int i = 0; i < files.size(); ++i) {
	        file = files.get(i);
	        if (!file.isEmpty()) {
	            try {
	                byte[] bytes = file.getBytes();
	                // 获取图片的文件名
	                String fileName = file.getOriginalFilename();
	                // 获取图片的扩展名
	                String extensionName = fileName.substring(fileName.lastIndexOf(".") + 1);
	                //产生随机随
	                int rand=(int)(1+Math.random()*(100-1+1));
	                // 新的图片文件名 = 获取时间戳+随机数"."图片扩展名
	                newFileName = String.valueOf(System.currentTimeMillis()) +rand+ "." + extensionName;
	                //图片路径
	                Path path = Paths.get(UPLOAD_FOLDER + newFileName);
					System.out.println("lj: " + path);
	                imgs.add(path);
	                //String dbpath="upload/"+newFileName;
	                //如果没有files文件夹，则创建
	                if (!Files.isWritable(path)) {
	                    Files.createDirectories(Paths.get(UPLOAD_FOLDER));
	                }
	                //文件写入指定路径
	                Files.write(path, bytes);
	            } catch (Exception e) {
	                return "图片上传失败 " + i + " => " + e.getMessage();
	            }
	        } else {
	            return "图片上传失败 " + i + " because the file was empty.";
	        }
	    }
	    return "200";
	}
	//分影院的添加
	@ApiOperation(value="添加分影院(参数：数据库存储路径，yname，yaddress东西太多)",notes="成功失败的结果")
	@RequestMapping("/add")
	@ResponseBody
	public String addCinema(String yname,String yaddress,String ytext,String yphone) {
		String dbpath="upload/"+newFileName;
		Cinema cinema=new Cinema();
		cinema.setYname(yname);
		yaddress=yaddress.replace("/","");
		cinema.setYaddress(yaddress);
		cinema.setYtext(ytext);
		cinema.setYimg(dbpath);
		cinema.setYphone(yphone);
		cinemaService.addCinema(cinema);
		return "添加成功";
	}
	//分影院信息修改
}

